package com.ksyun.ks3.services.handler;

import com.ksyun.ks3.exception.Ks3ClientException;
import com.ksyun.ks3.exception.Ks3ServiceException;
import com.ksyun.ks3.model.result.GetObjectTaggingResult;
import com.ksyun.ks3.services.request.Ks3HttpRequest;
import com.ksyun.ks3.services.request.tag.GetObjectTaggingRequest;
import com.ksyun.ks3.services.request.tag.ObjectTag;
import com.ksyun.ks3.services.request.tag.ObjectTagging;
import com.ksyun.ks3.util.ResponseUtils;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Headers;


public abstract class GetObjectTaggingResponseHandler extends Ks3HttpResponseHandler {

    public abstract void onFailure(GetObjectTaggingRequest request, Ks3ClientException clientException, Ks3ServiceException serviceException);

    public abstract void onSuccess(GetObjectTaggingRequest request, GetObjectTaggingResult result);

    @Override
    public final void onSuccess(Ks3HttpRequest request, int statusCode, Headers responseHeaders, byte[] response) {
        GetObjectTaggingResult result = new GetObjectTaggingResult();
        result.setStatusCode(statusCode);
        result.setHeaders(ResponseUtils.parseResponseHeaders(responseHeaders));
        result.setTagging(parseXml(response));
        this.onSuccess((GetObjectTaggingRequest) request, result);
    }

    @Override
    public final void onFailure(Ks3HttpRequest request, Ks3ClientException clientException, Ks3ServiceException serviceException) {
        this.onFailure((GetObjectTaggingRequest) request, clientException, serviceException);
    }

    @Override
    public final void onProgress(long bytesWritten, long totalSize) {
    }

    @Override
    public final void onStart() {
    }

    @Override
    public final void onFinish() {
    }

    @Override
    public final void onCancel() {
    }

    private ObjectTagging parseXml(byte[] response) {
        XmlPullParserFactory factory;
        ObjectTagging objectTagging = null;
        ArrayList<ObjectTag> tagList = null;
        ObjectTag tagging = null;
        try {
            factory = XmlPullParserFactory.newInstance();
            XmlPullParser parse = factory.newPullParser();
            parse.setInput(new ByteArrayInputStream(response), "UTF-8");
            int eventType = parse.getEventType();
            while (XmlPullParser.END_DOCUMENT != eventType) {
                String nodeName = parse.getName();
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        break;
                    case XmlPullParser.END_DOCUMENT:
                        break;
                    case XmlPullParser.START_TAG:
                        if ("Tagging".equalsIgnoreCase(nodeName)) {
                            objectTagging = new ObjectTagging();
                        }
                        if ("TagSet".equalsIgnoreCase(nodeName)) {
                            tagList = new ArrayList<>();
                            objectTagging.setTagSet(tagList);
                        }
                        if ("Tag".equalsIgnoreCase(nodeName)) {
                            tagging = new ObjectTag();
                            tagList.add(tagging);
                        }
                        if ("Key".equalsIgnoreCase(nodeName)) {
                            tagging.setKey(parse.nextText());
                        }
                        if ("Value".equalsIgnoreCase(nodeName)) {
                            tagging.setValue(parse.nextText());
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if (nodeName.equalsIgnoreCase("Tagging")) {

                        }
                        break;
                    case XmlPullParser.TEXT:

                        break;
                    default:
                        break;
                }
                eventType = parse.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return objectTagging;
    }
}
